package com.kizuki.algorithm.leetcode;

/**
 * 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
 *
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 */
public class 合并两个有序链表 {

    class Solution {
        public ListNode mergeTwoLists(ListNode left, ListNode right){
            ListNode head = new ListNode(0);
            ListNode cur = head;
            while(left != null && right != null) {
                if (left.val < right.val) {
                    cur.next = left;
                    left = left.next;
                } else {
                    cur.next = right;
                    right = right.next;
                }
                cur = cur.next;
            }
            if (left != null) {
                cur.next = left;
            }
            if (right != null) {
                cur.next = right;
            }
            return head.next;
        }

    }

}
